package front

import (
	"github.com/gorilla/websocket"
	"go-api-template/api/models/system"
	"go-api-template/service"
)

func (req ClientData) Do(ws *websocket.Conn, userId string) {
	var user system.User

	c := &connection{
		Sc: make(chan Message, 256),
		ws: ws,
		Data: &ClientData{
			UserId: userId,
		},
	}

	service.DB.Where("id = ?", userId).First(&user)
	c.Data.UserInfo.Name = user.Username

	// 写入连接池
	HUB.R <- c

	go c.writer()

	c.reader()
	defer func() {
		HUB.U <- c
	}()
}
